package com.demo.controller;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.demo.interceptor.BlogInterceptor;
import com.demo.po.Blog;
import com.demo.po.User;
import com.demo.util.SHA1;
import com.demo.validator.BlogValidator;
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.upload.UploadFile;

/**
 * BlogController
 * 所有 sql 写在 Model 或 Service 中，不要写在 Controller 中，养成好习惯，有利于大型项目的开发与维护
 */
@Before(BlogInterceptor.class)
public class BlogController extends Controller {
	
	public static String Token = "tianvo";
	
	public void index() {
		setAttr("blogPage", Blog.dao.paginate(getParaToInt(0, 1), 10));
		render("blog.html");
//		renderText("Hello,World");
	}
	
	public void add() {
	}
	
	
	public void weixin(){
		try{
			String signature = getPara("signature");
			String timestamp = getPara("timestamp");
			String nonce = getPara("nonce");
			String echostr = getPara("echostr");
			
			
			
	        List<String> params = new ArrayList<String>();  
	        params.add(Token);  
	        params.add(timestamp);  
	        params.add(nonce);  
	        //1. 将token、timestamp、nonce三个参数进行字典序排序   
	        Collections.sort(params, new Comparator<String>() {  
	             
	            public int compare(String o1, String o2) {  
	                return o1.compareTo(o2);  
	            }  
	        }); 
	        String temp = SHA1.encode(params.get(0) + params.get(1) + params.get(2));  
	        if (temp.equals(signature)) {  
	        	renderText(echostr);
	        }else{
	        	renderText("no match");
	        }  
	        			
		}catch(Exception e){
			renderText("request Error!");
		}

	}
	
	//上传文件
	public void upload(){
		List<UploadFile> files = getFiles("fileupload");
		UploadFile file = files.get(0);
		System.out.println(file.getFileName());
		Blog blog = new Blog();
		blog.set("title", "上传文件："+file.getFileName()+"        访问地址："+"upload/upload/"+file.getFileName());
		blog.set("content", "访问地址："+"upload/upload/"+file.getFileName());
		blog.save();
		redirect("/blog");
	}
	

	
	public void rendVelo(){
		setAttr("blogPage", Blog.dao.paginate(getParaToInt(0, 1), 10));
		renderVelocity("blogVolo.vm");
	}

	@Before(BlogValidator.class)
	public void save() {
		getModel(Blog.class).save();
		redirect("/blog");
	}
	
	public void edit() {
		setAttr("blog", Blog.dao.findById(getParaToInt()));
	}
	
	@Before(BlogValidator.class)
	public void update() {
		getModel(Blog.class).update();
		redirect("/blog");
	}
	
	public void delete() {
		Blog.dao.deleteById(getParaToInt());
		redirect("/blog");
	}
	
	public void tologin(){
		String returnUrl = getPara("returnUrl");
		setAttr("returnUrl", returnUrl);
		if(returnUrl.indexOf("showMapDialog") != -1){
			render("/user/login.html");
		}else{
			renderVelocity("/user/login.vm");
		}
		
	}
	
	public void login(){
		boolean loginok = false;
		String returnUrl = getPara("returnUrl");
		String username = getPara("username");
		String password = getPara("password");
		User user = new User();
		List<User> users = user.findUser(username);
		if(users != null && users.size() != 0){
			User u = users.get(0);
			String pass = u.get("password");
			if(pass.equals(password)){
				loginok = true;			
			}
		}
		
		if(loginok==true){
			setSessionAttr("username", username);
			if(StringUtils.isBlank(returnUrl)){
				renderVelocity("/mapdisplay/param.vm");
			}else{
				redirect(returnUrl);
			}
			
		}else{
			renderText("登陆失败！");
		}
		
	}
	
}


